package com.dm.library.http;

import com.dm.library.utils.DMLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: sushuo
 * 修改时间: 2017-05-16
 */
public abstract class BaseHttpParams {
    protected static final String TAG = BaseHttpParams.class.getCanonicalName();

    protected JSONObject jsonObject;

    protected BaseHttpParams() {
        init();
    }

    protected BaseHttpParams(Map<String, Object> source) {
        init();

        for (Map.Entry<String, Object> entry : source.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    protected BaseHttpParams(String key, Object value) {
        init();
        put(key, value);
    }

    protected BaseHttpParams(Object... keysAndValues) {
        init();
        int len = keysAndValues.length;
        if (len % 2 != 0)
            throw new IllegalArgumentException("Supplied arguments must be even");
        for (int i = 0; i < len; i += 2) {
            String key = String.valueOf(keysAndValues[i]);
            String val = String.valueOf(keysAndValues[i + 1]);
            put(key, val);
        }
    }

    private void init() {
        jsonObject = new JSONObject();
    }

    public void put(String key, Object value) {
        if (key != null && value != null) {
            try {
                jsonObject.put(key, value);
                DMLog.i(TAG, key + "=" + value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public void remove(String key) {
        jsonObject.remove(key);
    }

    public abstract String toString();

    public abstract String makeGetUrl(String url) throws UnsupportedEncodingException;
}